//
//  GoodsCommentCtr-cell.swift
//  Aiwei
//
//  Created by zhu xietong on 2018/1/5.
//  Copyright © 2018年 zhu xietong. All rights reserved.
//

import UIKit
import Eelay
import soapp






extension GoodsCommentCtr {
    
    class ImageItemKit1: EeScanSupportKit {
        override func addLayoutRules() {
            contentView.eelay = [
                [imageView,[ee.T.L.B.R]],
            ]
            imageView.ui.holder(type: .colorHex("#d1d1d1"))
        }
    }
    
    
    class CommentCell:EeImagesScanCell<ImageItemKit1>{
        //        typealias ImageKit = ImageItemKit
        let starV = FavStartView()
        let timeL = UILabel()
        
        let contentBk = UIView()
        let detailL = UILabel()
        
        
        var bt_constrain = NSLayoutConstraint()
        
        let scoreL = UILabel()
        var anonymousL = UILabel()
        override func addLayoutRules() {
            
            let blank = UIView()
            blank.backgroundColor = UIColor._tableBackground
            
            contentView.eelay = [
                [scoreL,[ee.T.L,[5,AppTheme.edge]],"20"],
                [starV,[scoreL,ee.Y.R,ee.Y.L,[0,8]],86,"14"],
                [contentBk,[scoreL,ee.B,ee.T,10],[ee.L.R,[AppTheme.edge,-AppTheme.edge]]],
                [timeL,[contentBk,ee.B,ee.T,10],[blank,ee.T,ee.B,-16],[ee.R,-AppTheme.edge]],
                [anonymousL,[ee.L,AppTheme.edge],[timeL,ee.Y]],
                [blank,[ee.B.L.R],"8"],
            ]
            
            scoreL.ui.font13.text(color: .color(light: "#666", dark: "#999")).text("\("评分".loc)：")
            anonymousL.ui.font13.text(color: .color(light: "#666", dark: "#999")).text("匿名评论".loc)
            _ = timeL.ui.font13.setTextColor(.color(light: "#666", dark: "#999")).right
            
            
            contentBk.eelay = [
                [detailL,[ee.T.L.R,[10,16,-16]]],
                [imgsView,[ee.L.B.R,[16,-10,-16]],[detailL,ee.B,ee.T,10]],
            ]
            
            
            bt_constrain = NSLayoutConstraint(item: detailL, attribute: .bottom, relatedBy: .equal, toItem: contentBk, attribute: .bottom, multiplier: 1, constant: -10)
            bt_constrain.priority = UILayoutPriority(rawValue:750)
            contentBk.addConstraint(bt_constrain)
            
            contentBk.backgroundColor = UIColor._tableBackground
            _ = contentBk.bsui.corner4
            contentView.backgroundColor = ._background
            _ = detailL.ui.font13.setTextColor(._foreground).line(0)
            
            
            
            imgsView.space = (8,8)
            imgsView.limit_width = Swidth - 2.0*AppTheme.edge - 32.0
            imgsView.que_num = (Swidth > 321) ? 5 : 4
            imgsView.item_scale = 1
            imgsView.setContentConstrainToHighLever()
            
            //            imgsView.padding = [0]
            
        }
        
        
        
        override func mo_model_reload() {
            
            let comment = DataNode.path(node: "__model.comment", mo.model, value: NSMutableDictionary())
            
            let thumbs = DataNode.path(node: "images",comment, value: NSMutableArray())
            if thumbs.count < 1{
                bt_constrain.priority = UILayoutPriority(rawValue:900)
            }
            else{
                bt_constrain.priority = UILayoutPriority(rawValue:750)
            }
            //图片预览相关--------------start
            let images = NSMutableArray()
            var imgs = [String]()
            
            thumbs.list { (url:String, i) in
                imgs.append(url)
                
                let one = ["img":url].mutable_dictionary
                images.add(one)
            }
            comment["__imgs"] = images
            imgsView.options_node = "__imgs"
            imgsView.mo.model = comment
            var _imgs = [EeImageResourceModel]()
            for one in imgs{
                let a = EeImageResourceModel(url: one)
                _imgs.append(a)
            }
            self.browerModels = _imgs
            

            //图片预览相关--------------end
            
            detailL.text = comment["content",""]
            let score = comment["level","1"]
            let score_f = Float(score) ?? 1
            let num = Int(score_f)
            starV.count = num
            
            timeL.text = stampToTime(comment["createtime",""])["yyyy年MM月dd日 HH:mm"]
            if comment["isanonymous","0"] == "1"
            {
                anonymousL.alpha = 1
            }
            else{
                anonymousL.alpha = 0
            }
        }
    }
    
    
    
    
}

extension GoodsCommentCtr{
    
    
    class OneGoodsField:FieldView{
        
        let imgV = UIImageView()
        let titleL = UILabel()
        let tagsL = UILabel()
        let priceL = UILabel()
        let numL = UILabel()
        override func addLayoutRules() {
            eelay = [
                [imgV,[ee.T.L.B],"50",50],
                [titleL,[imgV,ee.R,ee.L,8],[imgV,ee.T,4],[ee.R,-100]],
                [tagsL,[imgV,ee.R,ee.L,8],[imgV,ee.B,-4],[ee.R,-100]],
                [priceL,[ee.R],[titleL,ee.Y]],
                [numL,[ee.R],[tagsL,ee.Y]],
                //            [__line,[ee.L.B.R,[0,0.0]],"0.5"]
            ]
            backgroundColor = ._cellBackground
            //        __line.backgroundColor = FieldCell.separator_color
            
            //            imgV.img_url = __goods.one_str
            imgV.ui.holder(type: .grayBack).set(radius: 0, width: 0.4, color: UIColor(shex: AppTheme.border_hex))
            //            titleL.ui.font14.cl_222.text(__goodsNames.one_str)
            tagsL.ui.font12.cl_888.text("颜色：黑")
            priceL.ui.font12.setTextColor(._foreground).right.text("¥ 120")
            numL.ui.font12.cl_888.right.text("X1")
        }
        override func mo_model_reload() {
            
            let goods = DataNode.path(node: "__model", mo.model, value: NSMutableDictionary())
            
            imgV.img_url = goods["thumb",""]
            titleL.text = goods["title",""]
            tagsL.text = goods["optiontitle",""]
            priceL.text = "¥ " + goods[float:"price",0].price_display
            numL.text = "X " + goods["total","1"]
        }
        
    }
    
    
    class OneGoodsCell: JoTableCell {
        var field = OneGoodsField()
        
        override func addLayoutRules() {
            let whiteBK = UIView()
            contentView.eelay = [
                [whiteBK,[ee.T,10],[ee.B.R.L]],
                //            [__line,[ee.L.B.R,[10,0,-10]],"0.5"]
            ]
            whiteBK.backgroundColor = ._cellBackground
            whiteBK.eelay = [
                [field,[ee.T.L.B.R,[8,AppTheme.edge,-8,-AppTheme.edge]]],
            ]
            contentView.backgroundColor = UIColor._cellBackground
        }
        
        
        override func mo_model_reload() {
            
            if let f = DataNode<NSMutableDictionary>.path(node: "\(FK.fields).0", mo.model)
            {
                field.mo.model = f
            }
        }
    }
    
    class SumbmitCell: JoTableCell {
        let commitBt = UIButton()
        override func addLayoutRules() {
            
            let blank = UIView()
            contentView.eelay = [
                [blank,[ee.B.L.R],"8"],
                [commitBt,[ee.T.R,[10,-AppTheme.edge]],[blank,ee.T,ee.B,-10]],
                [__line,[ee.T.L.R,[0,AppTheme.edge,-AppTheme.edge]],"0.4"],
            ]
            
            __line.backgroundColor = UIColor._separator
            blank.backgroundColor = UIColor.table_bg_color
            commitBt.ui.font(size: 14).ntitle(title: "提交".loc).ncolor(color:___shop.tint).padding(insets: [5,14]).set(radius: 3, width: 1, color: ___shop.tint)
            commitBt.addTarget(self, action: #selector(commitAction), for: .touchUpInside)
        }
        
        @objc func commitAction() {
            active(ID: "submit", object: mo.model)
        }
    }
    
}

